<template>
  <div>
    <el-card>
      <el-form :inline="true" class="demo-form-inline" :model="from" ref="from">
        <el-form-item label="学科编号">
          <el-input v-model.trim="from.rid" style="width: 150px"></el-input>
        </el-form-item>
        <el-form-item label="学科名称">
          <el-input v-model.trim="from.name" style="width: 150px"></el-input>
        </el-form-item>
        <el-form-item label="创建者">
          <el-input
            v-model.trim="from.username"
            style="width: 150px"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model.trim="from.status" placeholder="请选择">
            <el-option
              v-for="item in employeesData.status"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button @click="close">清除</el-button>
          <el-button type="primary" @click="addEvent">+新增学科</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <template>
        <el-table :data="list" style="width: 100%">
          <el-table-column prop="id" type="index" width="50" label="序号">
          </el-table-column>
          <el-table-column prop="rid" label="学科编号"> </el-table-column>
          <el-table-column prop="name" label="学科名称" width="150">
          </el-table-column>
          <el-table-column prop="intro" label="学科简称" width="150">
          </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column prop="create_time" label="创建日期" width="180">
          </el-table-column>
          <el-table-column prop="status" label="状态">
            <template v-slot="{ row }">
              <div :class="{ textColor: row.status === 0 }">
                {{ row.status === 0 ? '禁用' : '启用' }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="300px">
            <template v-slot="{ row }">
              <el-button type="text" @click="editEvent(row)">编辑</el-button>
              <el-button type="text" @click="statusEvent(row)">启用</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            :current-page.sync="from.page"
            :page-size.sync="from.limit"
            :page-sizes="[1, 5, 10, 300]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="getData"
            @current-change="getData"
          />
        </div>
      </template>
    </el-card>
    <Add ref="add" @getData="getData"></Add>
  </div>
</template>
<script>
import employeesData from '@/api/constant/employess'
import { SubjectList, SubjectRemove, SubjectStatus } from '@/api/cyy'
import Add from './components/add.vue'
export default {
  components: {
    Add
  },
  data () {
    return {
      total: 10,
      employeesData: employeesData,
      list: [],
      from: {
        name: '', //	否	string	学科名称
        page: 1, //	否	string	页码 默认为1
        limit: 10, //	否	string	页尺寸 默认为10
        rid: '', //	否	string	学科编号
        username: '', //	否	string	创建者用户名
        status: '' //	否	string	状态 0(禁用) 1(启用)
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await SubjectList(this.from)
      //存储total
      this.total = res.data.pagination.total
      // this.page = res.data.pagination.page
      this.list = res.data.items
      console.log('学科列表', res)
    },
    //新增学科弹窗
    addEvent () {
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'add'
    },
    //删除学科
    del (row) {
      this.$confirm('您确定删除该数据吗？', '温馨提示')
        .then(async () => {
          const res = await SubjectRemove({
            id: row
          })
          console.log('row', res)
          //提示
          this.$message.success('删除成功')
          //刷新数据
          // 如果删除的数据页码>1 且当前页只有一条数据时，页码要往前一页
          if (this.from.page > 1 && this.list.length <= 1) {
            this.from.page--
          }
          this.getData()
        })
        .catch(() => {})
    },
    //编辑学科
    editEvent (row) {
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'edit'
      this.$refs.add.from = JSON.parse(JSON.stringify(row))
    },
    //清除
    close () {
      // 重置表单清除表单验证
      this.from = {
        name: '', //	否	string	学科名称
        // page: '', //	否	string	页码 默认为1
        // limit: '', //	否	string	页尺寸 默认为10
        rid: '', //	否	string	学科编号
        username: '', //	否	string	创建者用户名
        status: '' //	否	string	状态 0(禁用) 1(启用)
      }
    },
    //启用状态
    async statusEvent (row) {
      const res = await SubjectStatus({
        id: row.id
      })
      this.$message.success('状态更改成功')
      this.getData()
    },
    //搜索
    async search () {
      // console.log('搜索')
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header {
  color: #ffff;
  height: 54px;
  background: linear-gradient(90deg, #01c5fa, #1394fa);
  .el-dialog__title {
    height: 53px;
    color: #ffff;
    font-size: 16px;
  }
  .el-icon-close:before {
    color: #ffff;
  }
}
.textColor {
  color: red;
}
</style>
